package com.ruoyi.product.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.GoodsMapper;
import com.ruoyi.product.domain.Goods;
import com.ruoyi.product.service.IGoodsService;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-04
 */
@Service
public class GoodsServiceImpl implements IGoodsService 
{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisService redisCache;


    @Value("${cache.db.prefix:dbstore}")
    private String prefix;
    //单位分钟
    @Value("${cache.db.domain.goods.timeout:10}")
    private Long cacheTimeout;
    @Value("${cache.db.domain.goods.useCache:false}")
    private boolean useCache;
    @Value("${cache.db.domain.goods.strict:true}")
    private boolean strict;

    protected String cacheKey(Long id) {
        return new StringBuilder(prefix).append(":goods:").append(id).toString();
    }

    protected void deleteObject(Long id) {
        if (useCache) {
            if (id != null) {
                String key = cacheKey(id);
                try {
                    redisCache.deleteObject(key);
                } catch (Exception e) {
                    logger.error("redis delete error ,key:{}", key, e);
                    if(strict){
                        throw e;
                    }
                }
            }
        }

    }

    protected void setCacheObject(Goods goods) {
        if (useCache) {
            if (goods != null && goods.getId() != null) {
                String key = cacheKey(goods.getId());
                try {
                    if (goods != null) {
                        redisCache.setCacheObject(key, goods, cacheTimeout, TimeUnit.MINUTES);
                    }
                } catch (Exception e) {
                    logger.error("redis set error ,key:{}", key, e);

                }
            }
        }
    }

    protected Goods getCacheObject(Long id) {
        if (useCache) {
            String key = cacheKey(id);
            try {
                return redisCache.getCacheObject(key);
            } catch (Exception e) {
                logger.error("redis get error ,key:{}", key, e);

            }
        }

        return null;
    }

    /**
     * 查询商品
     * 
     * @param id 商品ID
     * @return 商品
     */
    @Override
    public Goods selectGoodsById(Long id)
    {
        Goods  goods = getCacheObject(id);
        if (goods == null) {
            goods = goodsMapper.selectGoodsById(id);
            setCacheObject(goods);
        }
        return goods;

    }
    @Override
    public List<Goods> selectGoodsByIds(Long[] ids)
    {
        return goodsMapper.selectGoodsByIds(ids);
    }

    /**
     * 查询商品列表
     * 
     * @param goods 商品
     * @return 商品
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods)
    {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int insertGoods(Goods goods)
    {

        goods.setCreateTime(DateUtils.getNowDate());
        try {
            return goodsMapper.insertGoods(goods);
        } finally {
            deleteObject(goods.getId());
        }
     }

    /**
     * 修改商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int updateGoods(Goods goods)
    {
        goods.setUpdateTime(DateUtils.getNowDate());
        try {
            return goodsMapper.updateGoods(goods);
        } finally {
            deleteObject(goods.getId());

        }
    }

    /**
     * 批量删除商品
     * 
     * @param ids 需要删除的商品ID
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] ids)
    {
        try {
            return goodsMapper.deleteGoodsByIds(ids);
        } finally {
            if (ids != null) {
                for (Long id : ids) {
                    deleteObject(id);

                }
            }
        }

    }

    /**
     * 删除商品信息
     * 
     * @param id 商品ID
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long id)
    {
        try {
            return goodsMapper.deleteGoodsById(id);
        } finally {
            deleteObject(id);
        }
    }


}
